package g11.testdao;

import g11.dao.AppointmentDAO;
import g11.dao.AppointmentDAOImpl;
import g11.dao.DAOException;
import g11.dao.DrugDAO;
import g11.dao.DrugDAOImpl;
import g11.dao.EmployeeDAO;
import g11.dao.EmployeeDAOImpl;
import g11.dao.PrescriptionDAO;
import g11.dao.PrescriptionDAOImpl;
import g11.dao.VisitationContentDAO;
import g11.dao.VisitationContentDAOImpl;
import g11.dao.VisitationDAO;
import g11.dao.VisitationDAOImpl;
import g11.model.Appointment;
import g11.model.Drug;
import g11.model.Employee;
import g11.model.Prescription;
import g11.model.Visitation;
import g11.model.VisitationContent;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

import org.junit.Test;

public class testPrescriptionDao extends TestCase {

	@Test
	public void testPrescriptionCRUD() throws DAOException {
		// Create and persistent Employee
		Employee employee = new Employee();
		employee.setUsername("penny");
		employee.setPassword("1234");
		Byte type = 1;
		employee.setType(type);
		employee.setName("Penny Money");
		employee.setDepartment("Surgery");
		employee.setTitle("Surgery Doctor");
		EmployeeDAO employeeDao = new EmployeeDAOImpl();
		employeeDao.createEmployee(employee);
		Employee retrievedEmployee = new Employee();
		retrievedEmployee = employeeDao.getEmployeeByUsername("penny");

		// create and persistent Appointment
		Appointment appointment = new Appointment();
		appointment.setDoctor(retrievedEmployee);
		AppointmentDAO appointmentDao = new AppointmentDAOImpl();
		appointmentDao.createAppointment(appointment);
		Appointment retrievedAppointment = new Appointment();
		retrievedAppointment = appointmentDao.getPendingAppointmentByDoctor(
				retrievedEmployee).get(0);

		// create and persisitent Visitation
		Visitation visitation = new Visitation();
		VisitationDAO visitationDao = new VisitationDAOImpl();
		visitationDao.createVisitationFromAppointment(retrievedAppointment);
		Visitation retrievedVisitation = new Visitation();
		retrievedVisitation = visitationDao
				.getCurrentVersionOfVisitation(retrievedAppointment
						.getAppointmentId());

		// create and persistent VisitationContent;
		VisitationContent visitationContent = new VisitationContent();
		visitationContent.setVisitation(retrievedVisitation);
		visitationContent.setComment("nothing");
		visitationContent.setDiagnosis("nothing");
		visitationContent.setTreatment("nothing");
		java.util.Date date = new java.util.Date();
		visitationContent.setLastModified(new Timestamp(date.getTime()));
		visitationContent.setVersion(retrievedVisitation.getCurrentVersion());
		VisitationContentDAO visitationContentDao = new VisitationContentDAOImpl();
		Integer visitationContentId = visitationContentDao
				.createVisitationContent(visitationContent);
		VisitationContent retrievedVisitationContent = new VisitationContent();
		retrievedVisitationContent = visitationContentDao
				.getVisitationContentById(visitationContentId);

		// create and persistent Drug;
		Drug drugAdvil = new Drug();
		drugAdvil.setDrugName("Advil");
		Byte drugType = 1;
		drugAdvil.setType(drugType);
		Drug drugTylenol = new Drug();
		drugTylenol.setDrugName("Tylenol");
		drugTylenol.setType(drugType);
		DrugDAO drugDao = new DrugDAOImpl();
		drugDao.createDrug(drugAdvil);
		drugDao.createDrug(drugTylenol);
		Drug retrievedDrugAdvil = new Drug();
		retrievedDrugAdvil = drugDao.getDrugByName(drugAdvil.getDrugName());
		Drug retrievedDrugTylenol = new Drug();
		retrievedDrugTylenol = drugDao.getDrugByName(drugTylenol.getDrugName());

		// Create Prescription for the VisitationContent;
		PrescriptionDAO prescriptionDao = new PrescriptionDAOImpl();
		prescriptionDao.createPrescription(retrievedVisitationContent,
				retrievedDrugAdvil, "1");
		Prescription retrievedPrescription = new Prescription();

		// verify retrieve
		retrievedPrescription = prescriptionDao
				.getPrescriptionByVisitationContentAndDrug(
						retrievedVisitationContent, drugAdvil);
		assertEquals(retrievedPrescription.getDrug().getDrugName(),
				drugAdvil.getDrugName());
		assertEquals(retrievedPrescription.getDosage(), "1");
		retrievedPrescription.setDosage("2");
		prescriptionDao.updatePrescription(retrievedPrescription);
		retrievedPrescription = prescriptionDao
				.getPrescriptionByVisitationContent(retrievedVisitationContent)
				.get(0);
		assertEquals(retrievedPrescription.getDosage(), "2");
		// add one more prescription
		prescriptionDao.createPrescription(retrievedVisitationContent,
				retrievedDrugTylenol, "3");
		assertEquals(
				prescriptionDao.getPrescriptionByVisitationContent(
						retrievedVisitationContent).size(), 2);
		retrievedPrescription = prescriptionDao
				.getPrescriptionByVisitationContent(retrievedVisitationContent)
				.get(1);
		assertEquals(retrievedPrescription.getDrug().getDrugName(), "Tylenol");
		assertEquals(retrievedPrescription.getDosage(), "3");

		// delete
		List<Prescription> prescriptionList = new ArrayList<Prescription>();
		prescriptionList = prescriptionDao
				.getPrescriptionByVisitationContent(retrievedVisitationContent);
		for (int i = 0; i < prescriptionList.size(); i++) {
			prescriptionDao.deletePrescription(prescriptionList.get(i));
		}
		drugDao.deleteDrug(retrievedDrugAdvil);
		drugDao.deleteDrug(retrievedDrugTylenol);
		visitationContentDao
				.deleteVisitationContent(retrievedVisitationContent);
		visitationDao.deleteVisitation(retrievedVisitation);
		appointmentDao.deleteAppointment(retrievedAppointment);
		employeeDao.deleteEmployee(retrievedEmployee);

	}
}